package com.feishi.core.util.common;

import java.io.*;
import java.util.*;

import static com.feishi.core.util.common.ByteUtil.BitByteConfig.INTEL;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2019-06-17
 * Time: 上午10:59
 */
public class CsvUtil {
    public static List<Map<String,String>> readListWithTitle(InputStream inputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String titleStr = reader.readLine();

        String[] titles = titleStr.split(",");
        String line;
        List<Map<String,String>> result=new ArrayList<>();
        while((line=reader.readLine())!=null){
            Map<String,String> item=new HashMap<>();
            String items[] = line.split(",");
            for (int i = 0; i < items.length && i < titles.length; i++) {
                item.put(titles[i],items[i]);
            }
            result.add(item);
        }
        return result;
    }
    public static List<List<String>> readList(InputStream inputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        List<List<String>> result=new ArrayList<>();
        while((line=reader.readLine())!=null){
            List<String> item= Arrays.asList(line.split(","));
            result.add(item);
        }
        return result;
    }

    public static void main(String[] args) throws IOException {
        System.out.println();
        System.out.println(readList(new FileInputStream(new File("/home/caixq/Desktop/解析/config.csv"))));
        List<Map<String, String>> configMap = readListWithTitle(new FileInputStream(new File("/home/caixq/Desktop/解析/config.csv")));
        List<ByteUtil.BitByteConfig> bitByteConfigs=new ArrayList<>();
        for (Map<String, String> map : configMap) {
            System.out.println(map);
            ByteUtil.BitByteConfig bitByteConfig=new ByteUtil.BitByteConfig();
            String name= map.get("name");
            String startBit= map.get("startBit");
            String size= map.get("size");
            String factor= map.get("factor");
            String offset= map.get("offset");
            String initialValue= map.get("initialValue");
            bitByteConfig.name=name;
            bitByteConfig.startBit=Integer.parseInt(startBit);
            bitByteConfig.size=Integer.parseInt(size);
            bitByteConfig.factor=factor==null?1:Double.parseDouble(factor);
            bitByteConfig.offset=factor==null?1:Double.parseDouble(offset);
            bitByteConfig.initialValue=initialValue==null?0:Double.parseDouble(initialValue);
            bitByteConfigs.add(bitByteConfig);
        }

        ByteUtil.BitByteBuild bitByteBuild=new ByteUtil.BitByteBuild(bitByteConfigs);
        bitByteBuild.setValue("vol",22);
        System.out.println(ByteUtil.getHexString(bitByteBuild.buildContent()));


        ByteUtil.BitByteReader bitByteReader=new ByteUtil.BitByteReader(bitByteConfigs,bitByteBuild.buildContent(),ByteUtil.BitByteConfig.INTEL);


        System.out.println(bitByteReader.getDouble("vol"));
        System.out.println(bitByteReader.getInt("vol"));

    }
}
